package com.bajiao.admin.controller.store;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.util.PasswordUtil;
import com.bajiao.common.util.oConvertUtils;
import com.bajiao.common.util.vans.RegexTool;
import com.bajiao.entity.store.StoreUser;
import com.bajiao.service.store.StorePermissionService;
import com.bajiao.service.store.StoreUserService;
import com.bajiao.vo.store.StorePermissionResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 商家账号
 * @Author: FanJq
 * @Date: 2024-12-24
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "商家账号")
@RestController
@RequestMapping("/store/user")
public class StoreUserController extends AdminBaseController<StoreUser, StoreUserService> {
    @Resource
    private StoreUserService storeUserService;
    @Resource
    private StorePermissionService permissionService;

    /**
     * @return
     * @description: 分页列表查询
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "商家账号-分页列表查询", notes = "商家账号-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String storeId, String keywords, String account) {
        startPage();
        List<StoreUser> resultList = storeUserService.getAdminList(storeId, keywords, account);
        return Result.ok(getDataTable(resultList));
    }

    /**
     * @param storeUser
     * @return
     * @description: 添加
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "商家账号-添加", notes = "商家账号-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StoreUser storeUser) {
        if (!RegexTool.mobileCheck(storeUser.getAccount())) {
            return Result.error("请填写正确的手机号码");
        }
        StoreUser dbModel = storeUserService.getByAccount(storeUser.getAccount());
        if (ObjectUtil.isNotNull(dbModel)) {
            return Result.error("手机号已注册");
        }
        if (storeUser.getType() == 1) {
            List<StorePermissionResponse> permissionList = permissionService.getPermissionList();
            List<String> codeList = permissionList.stream().map(StorePermissionResponse::getCode).collect(Collectors.toList());
            storeUser.setPermission(String.join(StringPool.COMMA, codeList));
        }
        String salt = oConvertUtils.randomGen(8);
        storeUser.setSalt(salt);
        String encrypt = PasswordUtil.encrypt(storeUser.getPassword(), "", salt);
        storeUser.setPassword(encrypt);
        storeUserService.save(storeUser);
        return Result.ok("添加成功！");
    }

    public static void main(String[] args) {
        System.out.println(PasswordUtil.encrypt("123456", "", "6mK60l10"));
    }

    /**
     * @param storeUser
     * @return
     * @description: 编辑
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "商家账号-编辑", notes = "商家账号-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreUser storeUser) {
        storeUserService.updateById(storeUser);
        return Result.ok("编辑成功!");
    }

    @ApiOperation(value = "商家账号-编辑", notes = "商家账号-编辑")
    @PutMapping(value = "/edit/password")
    public Result<?> editPassword(@RequestBody StoreUser request) {
        StoreUser storeUser = storeUserService.getByAccount(request.getAccount());
        String salt = oConvertUtils.randomGen(8);
        storeUser.setSalt(salt);
        String encrypt = PasswordUtil.encrypt(request.getPassword(), "", salt);
        storeUser.setPassword(encrypt);
        storeUserService.updateById(storeUser);
        return Result.ok("重置成功");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "商家账号-通过id删除", notes = "商家账号-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        storeUserService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * @param ids
     * @return
     * @description: 批量删除
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "商家账号-批量删除", notes = "商家账号-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.storeUserService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "商家账号-通过id查询", notes = "商家账号-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StoreUser storeUser = storeUserService.getById(id);
        return Result.ok(storeUser);
    }

    /**
     * @param request
     * @param storeUser
     * @description: 导出excel
     * @author: FanJq
     * @date: 2024-12-24
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StoreUser storeUser) {
        return super.exportXls(request, storeUser, StoreUser.class, "商家账号");
    }

    /**
     * @param request
     * @param response
     * @return
     * @description: 通过excel导入数据
     * @author: FanJq
     * @date: 2024-12-24
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StoreUser.class);
    }

}
